package com.web.cms.pan.service;

import com.blue.core.id.IdGenerator;
import com.blue.core.util.RandomUtil;
import com.blue.jdbc.JdbcDataSource;
import com.blue.jdbc.Page;
import com.web.cms.pan.dao.OperatorLogDao;
import com.web.cms.pan.dao.ShareDao;
import com.web.cms.pan.dao.ShareLogDao;
import com.web.cms.pan.model.Share;
import com.web.cms.pan.model.ShareLog;
import com.web.core.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 共享业务层
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年04月09日
 */
@Service("panShareService")
@Transactional
@JdbcDataSource(Const.DS_CMS)
public class ShareService
{
	@Autowired
	private ShareDao shareDao;
	@Autowired
	private OperatorLogDao operatorLogDao;
	@Autowired
	private ShareLogDao shareLogDao;
	
	public ShareService()
	{
	}

	public void add(Share share)
	{
		share.setCode(IdGenerator.uuid16bit());
		if (share.getPassword() == null || share.getPassword().isEmpty())
			share.setCode(RandomUtil.rand(RandomUtil.RandomType.UPPER_LOWER_CASE, 6));

		shareDao.save(share);
	}
	
	public Share save(Share share)
	{
		if (share.getId() == null)
		{
			shareDao.save(share);
		}
		else
		{
			shareDao.update(share);
		}
		Share a = shareDao.get(share.getId());
		return a;
	}
	
	public Share get(Integer id)
	{
		Share share = shareDao.get(id);
		return share;
	}
	
	public Page<Share> list(Share share, Page<Share> page)
	{
		page = shareDao.list(share, page);
		return page;
	}
	
	public List<Share> list(Share share)
	{
		List<Share> list = shareDao.list(share);
		return list;
	}
	
	public void delete(Share share)
	{
		shareDao.delete(share);
	}

	public Page<ShareLog> listLog(ShareLog shareLog, Page<ShareLog> page)
	{
		page = shareLogDao.list(shareLog, page);
		return page;
	}
	
}
